package com.kotlin.jetpack.android.jssdk

import android.app.Activity
import android.text.TextUtils
import android.webkit.JavascriptInterface
import android.webkit.WebView
import com.google.gson.Gson
import com.kotlin.android.ktx.utils.LogUtils
import com.kotlin.jetpack.android.jssdk.bean.JumpToBean
import com.kotlin.jetpack.android.jssdk.bean.ShareToBean
import com.kotlin.jetpack.android.jssdk.jsobj.JSJumpToObj
import com.kotlin.jetpack.android.jssdk.jsobj.JSPlatformObj
import com.kotlin.jetpack.android.jssdk.jsobj.JSShareToObj
import com.kotlin.jetpack.android.jssdk.jsobj.JSWhetherLoginObj
import com.kotlin.jetpack.android.jssdk.listener.JSJumpToListener
import com.kotlin.jetpack.android.jssdk.listener.JSShareToListener

/**
 * 创建者: zl
 * 创建时间: 2020/5/18 10:57 AM
 * 描述: JSSDK主轴
 */
class SDKWarp {
    private var context: Activity? = null
    private var webView: WebView? = null

    constructor(context: Activity, webView: WebView) {
        this.context = context
        this.webView = webView
        initJSObjs(context)
    }

    private fun initJSObjs(context: Activity) {
        jumpToObj = JSJumpToObj()
    }

    private lateinit var jumpToObj: JSJumpToObj
    private lateinit var whetherLoginObj: JSWhetherLoginObj
    private lateinit var platformObj: JSPlatformObj
    private lateinit var shareToObj: JSShareToObj


    fun setJumpToListener(jumpToListener: JSJumpToListener?) {
        jumpToObj.listener = jumpToListener
    }

    fun setShareToListener(listener: JSShareToListener?) {
        shareToObj.listener = listener
    }

    /**
     * wanda H5公共跳转入口
     *
     * @param jsonStr
     */
    @JavascriptInterface
    fun jumpTo(jsonStr: String) {
        LogUtils.d("jumpTo-> $jsonStr")
        if (!TextUtils.isEmpty(jsonStr)) {
            val obj: Any = handleJson(jsonStr, JumpToBean::class.java)
            val jumpToBean: JumpToBean = obj as JumpToBean
            jumpToObj.jumpTo(jumpToBean)
        }
    }


    /**
     * wanda H5 登录信息
     *
     * @return
     */
    @JavascriptInterface
    fun whetherLogin(jsonStr: String?): String? {
        return whetherLoginObj.whetherLogin
    }

    /**
     * 设置平台类型
     *
     * @param jsonStr
     * @return
     */
    @JavascriptInterface
    fun platform(jsonStr: String?): String? {
        return if (platformObj != null) {
            platformObj.getPlatform()
        } else "{}"
    }

    /**
     * H5回调native 第三方分享
     *
     * @param jsonStr
     * @return
     */
    @JavascriptInterface
    fun shareTo(jsonStr: String) {
        LogUtils.e("shareTo-> $jsonStr")
        if (!TextUtils.isEmpty(jsonStr)) {
            val obj: Any = handleJson(jsonStr, ShareToBean::class.java)
            val shareToBean: ShareToBean = obj as ShareToBean
            LogUtils.e("shareToBean-> $shareToBean")
            shareToObj.shareTo(shareToBean)
        }
    }


    private fun handleJson(data: String?, className: Class<*>?): Any {
        val gson = Gson()
        return gson.fromJson(data, className)
    }
}